package edu.nanyang.struts;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;

import edu.nanyang.domain.City;
import edu.nanyang.domain.User;
import edu.nanyang.hibernate.service.IGoodsService;
import edu.nanyang.util.CommonData;
import edu.nanyang.util.GoodsHelper;
import edu.nanyang.util.QueryResult;

public class GoodsAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int page;
	private int pageSize;
	private long total;
	private List<GoodsHelper> goods;
	private String buyOrSale;
	private int schoolId;
	private String goodsType;
	private double priceFrom;
	private double priceTo;
	private boolean lookOnlineOnly;
	private String sortType;
	private String queryStringdetail;
	private int queryDays;
	private GoodsHelper goodHelper;
	private IGoodsService goodsService;
	private int goodsId;
	private int shenHe;
	
	private int userId;
	private String result;
	public String getGoodsByJingpaiUserid(){
		User user = (User)ServletActionContext.getRequest().getSession().getAttribute("user");
		System.out.println("user============="+user);
		if(user!=null){
			goods=this.goodsService.getGoodsByJingpaiUserid(user.getId());
		}else{
			goods=new ArrayList<GoodsHelper>();
		}
		return SUCCESS;
	}
	public String refreshGoods(){
		if(this.goodsService.refreshGoods(goodsId)){
			result="刷新成功";
		}else{
			result="刷新失败";
		}
		return SUCCESS;
	}
	public String shenHeGoods(){
		if(this.goodsService.shenHeGoods(goodsId, shenHe)){
			result="操作成功！";
		}else{
			result="操作失败";
		}
		return SUCCESS;
	}
	@JSON(serialize=false)
	public String getGoodsUnvirified(){
		goods=this.goodsService.getGoodsUnverified();
		return SUCCESS;
	}
	public String saleGoods(){
		if(goodsId==0 || userId==0){
			result="拍出商品失败，物品或者指定用户找不到";
			return SUCCESS;
		}
		if(!this.goodsService.saleGoods(goodsId, userId)){
			result="系统错误，拍出商品失败，请联系管理员";
		}
		result="拍出商品成功";
		return SUCCESS;
	}
	
	@JSON(serialize = false)
	public String getMyGoods(){
		User user = (User)ServletActionContext.getRequest().getSession().getAttribute("user");
		if(user!=null){
			goods=this.goodsService.getGoodsByUser(user);
		}else{
			goods=new ArrayList<GoodsHelper>();
		}
		return SUCCESS;
	}
	@JSON(serialize = false)
	public String getGoodsByDetail(){

		if(page==0){page=CommonData.DEFAULT_PAGE;}
		if(pageSize==0){pageSize=CommonData.DEFAULT_PAGE_SIZE;}
		QueryResult<GoodsHelper> queryResult=null;
		int cityID = CommonData.DEFAULT_CITY_ID;
		HttpSession session = ServletActionContext.getRequest().getSession();
		Object o = session.getAttribute("city");
		if (o != null) {
			cityID = ((City) o).getId();
		}
		try{
		queryResult=this.goodsService.acquireGoodsByDetail(buyOrSale, 
																			  schoolId, 
															            	  goodsType, 
																			  priceFrom, 
																			  priceTo, 
																			  lookOnlineOnly, 
																			  sortType, 
																			  queryStringdetail, 
																			  queryDays, 
																			  page, 
																			  pageSize,
																			  cityID);
		}catch(Exception e){
			e.printStackTrace();
		}
		goods=queryResult.getObjects();
		total=queryResult.getTotal();
		page=queryResult.getPage();
		pageSize=queryResult.getPageSize();
		
		return SUCCESS;
	}

	@JSON(serialize = false)
	public String getGoodsByCurrCity() {
		int cityID = CommonData.DEFAULT_CITY_ID;
		if(page==0){page=CommonData.DEFAULT_PAGE;}
		if(pageSize==0){pageSize=CommonData.DEFAULT_PAGE_SIZE;}
		HttpSession session = ServletActionContext.getRequest().getSession();
		Object o = session.getAttribute("city");
		if (o != null) {
			cityID = ((City) o).getId();
		}
		total=this.goodsService.acquireTotalGoodsNumByCurrCityID(cityID);
		goods=this.goodsService.acquireGoodsByCurrCity(cityID, page, pageSize);
		return SUCCESS;
	}

	public String acquireGoodsById(){
		goodHelper=this.goodsService.acquireGoodsById(goodsId);
		return SUCCESS;
	}
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public List<GoodsHelper> getGoods() {
		return goods;
	}

	public void setGoods(List<GoodsHelper> goods) {
		this.goods = goods;
	}

	public void setGoodsService(IGoodsService goodsService) {
		this.goodsService = goodsService;
	}

	public String getBuyOrSale() {
		return buyOrSale;
	}

	public void setBuyOrSale(String buyOrSale) {
		this.buyOrSale = buyOrSale;
	}

	public int getSchoolId() {
		return schoolId;
	}

	public void setSchoolId(int schoolId) {
		this.schoolId = schoolId;
	}

	public String getGoodsType() {
		return goodsType;
	}

	public void setGoodsType(String goodsType) {
		this.goodsType = goodsType;
	}

	public double getPriceFrom() {
		return priceFrom;
	}

	public void setPriceFrom(double priceFrom) {
		this.priceFrom = priceFrom;
	}

	public double getPriceTo() {
		return priceTo;
	}

	public void setPriceTo(double priceTo) {
		this.priceTo = priceTo;
	}

	public boolean isLookOnlineOnly() {
		return lookOnlineOnly;
	}

	public void setLookOnlineOnly(boolean lookOnlineOnly) {
		this.lookOnlineOnly = lookOnlineOnly;
	}

	public String getSortType() {
		return sortType;
	}

	public void setSortType(String sortType) {
		this.sortType = sortType;
	}

	public String getQueryStringdetail() {
		return queryStringdetail;
	}

	public void setQueryStringdetail(String queryStringdetail) {
		this.queryStringdetail = queryStringdetail;
	}

	public int getQueryDays() {
		return queryDays;
	}

	public void setQueryDays(int queryDays) {
		this.queryDays = queryDays;
	}

	public GoodsHelper getGoodHelper() {
		return goodHelper;
	}

	public void setGoodHelper(GoodsHelper goodHelper) {
		this.goodHelper = goodHelper;
	}

	public int getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(int goodsId) {
		this.goodsId = goodsId;
	}
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public int getShenHe() {
		return shenHe;
	}
	public void setShenHe(int shenHe) {
		this.shenHe = shenHe;
	}
	
}
